﻿using ASP;
using ClassLibrary.BusinessLayer.Facade;
using IPMS.AccessAuth;
using IPMS.BLL;
using Resources;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.Common;
using System.Data.SqlClient;
using System.Globalization;
using System.Threading;
using System.Web.Profile;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web;
using SysOperationLayer.DBHandle;
using SysComm;


public partial class ProjectClassUpdate : Page, IRequiresSessionState
{
    LogDB m_logdb = new LogDB();
    protected void btnCancel_Click(object sender, EventArgs e)
    {
        this.clearText();
        this.setButtons(string.Empty);
        this.setTextReadOnly(false);
    }

    protected void btnDelete_Click(object sender, EventArgs e)
    {
        Auth canDelete = new Auth(this.Session["UserId"].ToString(), string.Empty, "ProjectClass");
        if (!canDelete.CanDelete())
        {
            this.lblError.Visible = true;
            this.lblError.Text = Messages.mNoDelete.ToString();
        }
        else
        {
            try
            {
                string trueUpdateStr = string.Empty;
                string falseUpdateStr = string.Empty;
                if (Session["Culture"] != null)
                {

                    if (Session["Culture"].ToString() == "zh-TW")
                    {
                        trueUpdateStr = "刪除成功!";
                        falseUpdateStr = "刪除失敗!";

                    }
                    else if (Session["Culture"].ToString() == "zh-CN")
                    {
                        trueUpdateStr = "删除成功!";
                        falseUpdateStr = "删除失败!";

                    }
                    else
                    {
                        trueUpdateStr = "Delete succeed!";
                        falseUpdateStr = "Delete failed!";

                    }
                }

                this.ODSProjectClass.DeleteMethod = "Delete";
                this.ODSProjectClass.DeleteParameters["IsDelete"].DefaultValue = "true";
                this.ODSProjectClass.DeleteParameters["DeleteBy"].DefaultValue = this.Page.User.Identity.Name.ToString();
                this.ODSProjectClass.DeleteParameters["DeleteIP"].DefaultValue = HttpContext.Current.Request.UserHostAddress;
                this.ODSProjectClass.DeleteParameters["ProjectClass"].DefaultValue = this.txtProjectClass.Text.ToString();
                if (this.ODSProjectClass.Delete() < 0)
                {
                    IPMS.Common.Jscript.AlertAndRedirect(trueUpdateStr, "OptProjectClass.aspx", this.Page);
                    m_logdb.writeSucLog(SysUtil.m_functiong_projectclass, "delete project class information succeed!");
                }
                else
                {
                    IPMS.Common.Jscript.Alert(falseUpdateStr, this.Page);
                    m_logdb.writeFaileLog(SysUtil.m_functiong_projectclass, "delete project class information faile!");
                }    
                this.clearText();
                //this.Search();
                this.setButtons(string.Empty);
                this.setTextReadOnly(false);
            }
            catch (Exception ex)
            {
                List<string> lstValues = new List<string>();
                //this.lblError.Text = IPDataFormat.GenerateMessage(ex, lstValues);
                this.lblError.Text = ex.Message.ToString();
                //this.chkForOperation.Checked = true;
            }
        }
    }

    protected void btnModify_Click(object sender, EventArgs e)
    {
        Auth canDelete = new Auth(this.Session["UserId"].ToString(), string.Empty, "ProjectClass");
        if (!canDelete.CanUpdate())
        {
            this.lblError.Visible = true;
            this.lblError.Text = Messages.mNoEdit.ToString();
        }
        else
        {
            this.setButtons("modify");
            this.setTextReadOnly(false);
            this.txtProjectClass.ReadOnly = true;
            this.txtDescription.Focus();
        }
    }

    protected void btnNew_Click(object sender, EventArgs e)
    {
        Auth canDelete = new Auth(this.Session["UserId"].ToString(), string.Empty, "ProjectClass");
        if (!canDelete.CanAdd())
        {
            this.lblError.Visible = true;
            this.lblError.Text = Messages.mNoAdd.ToString();
        }
        else
        {
            this.setButtons("new");
            this.clearText();
            this.setTextReadOnly(false);
            this.txtProjectClass.Focus();
        }
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        string trueUpdateStr = string.Empty;
        string falseUpdateStr = string.Empty;
        string trueInsertStr = string.Empty;
        string falseInsertStr = string.Empty;
        if (Session["Culture"] != null)
        {

            if (Session["Culture"].ToString() == "zh-TW")
            {
                trueUpdateStr = "更新成功!";
                falseUpdateStr = "更新失败!";
                trueInsertStr = "添加成功!";
                falseInsertStr = "添加失敗!";
            }
            else if (Session["Culture"].ToString() == "zh-CN")
            {
                trueUpdateStr = "更新成功!";
                falseUpdateStr = "更新失败!";
                trueInsertStr = "添加成功!";
                falseInsertStr = "添加失败!";
            }
            else
            {
                trueUpdateStr = "Update succeed!";
                falseUpdateStr = "Update failed!";
                trueInsertStr = "Add succeed!";
                falseUpdateStr = "Add failed!";
            }
        }

        if (this.cbForUpdate.Checked)
        {
            this.ODSProjectClass.UpdateMethod = "Update";
            this.ODSProjectClass.UpdateParameters["Description"].DefaultValue = this.txtDescription.Text;
            this.ODSProjectClass.UpdateParameters["UpdateBy"].DefaultValue = this.Page.User.Identity.Name.ToString();
            this.ODSProjectClass.UpdateParameters["ProjectClass"].DefaultValue = this.txtProjectClass.Text;
            this.ODSProjectClass.UpdateParameters["UpdateIP"].DefaultValue = HttpContext.Current.Request.UserHostAddress;

            if (this.ODSProjectClass.Update() < 0)
            {
                IPMS.Common.Jscript.AlertAndRedirect(trueUpdateStr, "OptProjectClass.aspx", this.Page);
                m_logdb.writeSucLog(SysUtil.m_functiong_projectclass, "update project class information succeed!");
            }
            else
            {
                IPMS.Common.Jscript.Alert(falseUpdateStr, this.Page);
                m_logdb.writeFaileLog(SysUtil.m_functiong_projectclass, "update project class information faile!");
            }
        }
        else
        {
            try
            {
                this.ODSProjectClass.InsertMethod = "Insert";
                this.ODSProjectClass.InsertParameters["ProjectClass"].DefaultValue = this.txtProjectClass.Text;
                this.ODSProjectClass.InsertParameters["Description"].DefaultValue = this.txtDescription.Text;
                this.ODSProjectClass.InsertParameters["CreateBy"].DefaultValue = this.Page.User.Identity.Name.ToString();
                this.ODSProjectClass.InsertParameters["UpdateBy"].DefaultValue = this.Page.User.Identity.Name.ToString();
                this.ODSProjectClass.InsertParameters["OwnerGroupID"].DefaultValue = this.Session["OwnerGroupID"].ToString();
                this.ODSProjectClass.InsertParameters["CreateIP"].DefaultValue = HttpContext.Current.Request.UserHostAddress;

                if (this.ODSProjectClass.Insert() < 0)
                {
                    IPMS.Common.Jscript.AlertAndRedirect(trueInsertStr, "OptProjectClass.aspx", this.Page);
                    m_logdb.writeSucLog(SysUtil.m_functiong_projectclass, "add project class succeed!");
                }
                else
                {
                    IPMS.Common.Jscript.Alert(falseInsertStr, this.Page);
                    m_logdb.writeFaileLog(SysUtil.m_functiong_projectclass, "add project class faile!");
                }
                this.setTextReadOnly(false);
                this.setButtons(string.Empty);
                this.cbForUpdate.Checked = true;
                this.clearText();
            }
            catch (Exception ex)
            {
                List<string> lstValues = new List<string>();
                //this.lblError.Text = IPDataFormat.GenerateMessage(ex, lstValues);
                this.lblError.Text = ex.Message.ToString();
                this.chkForOperation.Checked = true;
                this.setButtons("new");
            }
        }
        //this.Search();
    }

    //protected void btnSearch_Click(object sender, EventArgs e)
    //{
    //    this.Search();
    //}

    protected void clearText()
    {
        this.txtProjectClass.Text = string.Empty;
        this.txtDescription.Text = string.Empty;
        this.cbForUpdate.Checked = true;
        this.chkForOperation.Checked = false;
        this.lblError.Text = "";
    }

    //protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    //{
    //    this.GridView1.PageIndex = e.NewPageIndex;
    //    this.GridView1.DataBind();
    //    this.Search();
    //}

    //protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
    //{
    //    this.chkForOperation.Checked = true;
    //    this.cbForUpdate.Checked = true;
    //    ProjectClasses runMethod = new ProjectClasses();
    //    runMethod.GetProjectClassInfo(this.GridView1.DataKeys[this.GridView1.SelectedIndex].Values["ProjectClass"].ToString());
    //    this.txtProjectClass.Text = this.GridView1.DataKeys[this.GridView1.SelectedIndex].Values["ProjectClass"].ToString();
    //    this.txtDescription.Text = runMethod.Description;
    //    this.setButtons(string.Empty);
    //}

    protected override void InitializeCulture()
    {
        string sCulture = ConfigurationManager.AppSettings["DefaultCulture"].ToString();
        try
        {
            sCulture = this.Session["Culture"].ToString();
        }
        catch (NullReferenceException)
        {
        }
        Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(sCulture);
        Thread.CurrentThread.CurrentUICulture = new CultureInfo(sCulture);
        base.InitializeCulture();
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        Auth canView = new Auth(this.Session["UserId"].ToString(), string.Empty, "ProjectClass");
        if (!canView.CanUpdate())
        {
            base.Response.Redirect("ErrorPage/NotAllowed.aspx");
        }
        else
        {
            new Global().ShowError(this.Page, false);
            this.setButtons(string.Empty);
            this.setTextReadOnly(false);
            if (!this.Page.IsPostBack)
            {
                ((Label)this.Master.FindControl("lblMstTitle")).Text = "| " + ISitemap.MaintenanceFiles.ToString() + " | " + ISitemap.ProjectClassUpdate.ToString();

                ProjectClasses runMethod = new ProjectClasses();
                  runMethod.GetProjectClassInfo(Request["ProjectClass"].ToString());
                    this.txtProjectClass.Text = Request["ProjectClass"].ToString();
                    this.txtDescription.Text = runMethod.Description;

                    ViewState["isRestore"] = false;

                    if (runMethod.IsDelete)
                    {
                        if (Session["SuperUser"] != null)
                        {
                            if (Convert.ToBoolean(Session["SuperUser"]))
                            {
                                ViewState["isRestore"] = true;
                            }
                        }
                    }

                    bool isRestore = Convert.ToBoolean(ViewState["isRestore"]);

                    if (isRestore)
                    {
                        this.setButtons("restore");
                        return;
                    }

                   this.setButtons(string.Empty);
                   cbForUpdate.Checked = true;
                this.btnDelete.Attributes.Add("onClick", "return confirm('" + Messages.DeleteConfirmation.ToString() + "')");
            }
        }
    }

    //protected void Search()
    //{
    //    ProjectClassFacade facade = new ProjectClassFacade();
    //    SqlParameter[] sqlparam = new SqlParameter[2];
    //    sqlparam[0] = new SqlParameter();
    //    sqlparam[0].ParameterName = "@ProjectClass";
    //    sqlparam[0].Value = this.txtProjectClassSearch.Text.ToString();
    //    sqlparam[1] = new SqlParameter();
    //    sqlparam[1].ParameterName = "@Description";
    //    sqlparam[1].Value = this.txtProjectClassDescSearch.Text.ToString();
    //    DbParameter[] param = sqlparam;
    //    DataView dv = facade.GetsProjectClass("_IPMS_ProjectClassSearch_SP", param).DefaultView;
    //    this.GridView1.DataSourceID = string.Empty;
    //    this.GridView1.DataSource = dv;
    //    this.GridView1.DataBind();
    //}

    public void setButtons(string action)
    {
        //switch (action)
        //{
        //    case "new":
        //        this.btnNew.Enabled = false;
        //        this.btnModify.Enabled = false;
        //        this.btnSave.Enabled = true;
        //        this.btnDelete.Enabled = false;
        //        return;

        //    case "modify":
        //        this.btnNew.Enabled = false;
        //        this.btnModify.Enabled = false;
        //        this.btnSave.Enabled = true;
        //        this.btnDelete.Enabled = false;
        //        return;
        //}

        switch (action)
        {
            case "restore":
                this.btnNew.Enabled = false;
                this.btnModify.Enabled = false;
                this.btnSave.Enabled = false;
                this.btnDelete.Visible = false;
                this.btnRestore.Visible = true;
                return;
        }

        this.btnNew.Enabled = true;
        this.btnModify.Enabled = this.txtProjectClass.Text.Length != 0;
        this.btnSave.Enabled = true;
        this.btnDelete.Enabled = this.txtProjectClass.Text.Length != 0;
        this.btnRestore.Visible = false;
    }

    protected void btnRestore_Click(object sender, EventArgs e)
    {
        ProjectClasses projectClasses = new ProjectClasses();

        string updateBy = this.Page.User.Identity.Name.ToString();
        string updateIP = HttpContext.Current.Request.UserHostAddress;

        bool isSuccess = projectClasses.Update(false, updateBy, updateIP, this.txtProjectClass.Text.Trim());

        string trueUpdateStr = string.Empty;
        string falseUpdateStr = string.Empty;
        if (Session["Culture"] != null)
        {

            if (Session["Culture"].ToString() == "zh-TW")
            {
                trueUpdateStr = "恢復成功!";
                falseUpdateStr = "恢復失敗!";

            }
            else if (Session["Culture"].ToString() == "zh-CN")
            {
                trueUpdateStr = "恢复成功!";
                falseUpdateStr = "恢复失败!";

            }
            else
            {
                trueUpdateStr = "Restore succeed!";
                falseUpdateStr = "Restore failed!";

            }
        }

        if (isSuccess)
        {
            IPMS.Common.Jscript.AlertAndRedirect(trueUpdateStr, "OptProjectClass.aspx", this.Page);
        }
        else
        {
            IPMS.Common.Jscript.Alert(falseUpdateStr, this.Page);
        }
    }

    protected void setTextReadOnly(bool bBool)
    {
        //this.txtProjectClass.ReadOnly = bBool;
        this.txtDescription.ReadOnly = bBool;
    }

    protected void btnBack_Click(object sender, EventArgs e)
    {
        Response.Redirect("OptProjectClass.aspx");
    }
   
}
